import cv2
import numpy as np

path = "Resources/cs.png"
config_path = "Resources/frozen_inference_graph.pb"
weight_path = "Resources/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
className_path = "Resources/coco.names"
confThreshold = 0.4
nms_threshold = 0.1

img = cv2.imread(path)
img = cv2.resize(img, (1680, 1050))
shape = img.shape

classNames = []
with open(className_path,'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')

# 加载模型的各种参数，当让我也不知道啥意思
net = cv2.dnn_DetectionModel(config_path, weight_path)
net.setInputSize(shape[0], shape[1])
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)

# 检测图像
classIds, confs, bbox = net.detect(img, confThreshold=confThreshold)

# 非极大值抑制
indices = cv2.dnn.NMSBoxes(bbox, confs, confThreshold, nms_threshold=nms_threshold)

print("indeices:",indices)

for i in indices:
    print("i:",i)
    id,conf,box = classIds[i],confs[i],bbox[i]
    cv2.rectangle(img, (box[0],box[1]), (box[0]+box[2], box[1]+box[3]), (255, 0, 0), 3)
    cv2.putText(img, classNames[id-1].upper(), (box[0]+10, box[1]+30), cv2.FONT_HERSHEY_COMPLEX, 0.4, (0, 255, 0), 2)

cv2.imshow("Output", img)
cv2.waitKey(0)


















